﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Filmek
{
    public partial class ModositFilm : Form
    {
        private String itemToModify;
        private int itemToModifyID;
		private String itemTypeToModify;
        private bool Err;
        private MyReader myReader;

		//regi adatok a filmrol
		private String regiFilmNev;
		private String regiFilmLeiras;
		private String regiKiDat;
		private int regiAr;
		private float regiErtekeles;
		private int regiHossz;
		private int regiMostID;

		private String regiRendezoNev;
		private String regiRendezoLeiras;
		private String regiRendezoSzulDat;
		private String regiNemzetiseg;

		public String regiSzineszNev;
		public String regiSzineszSzulHely;
		public String regiSzineszSzulDat;
		public int regiNem;
		



        public ModositFilm()
        {
            InitializeComponent();
        }

		//betolti a hatter kepet
		private void loadBgImage()
		{
			Image image1 = new Bitmap(@"E:\Dropbox\Egyetem\II ev\I felev\Adatbazis\bgImage22.jpg", true);
			//image1.Size = this.Size;
			this.BackgroundImage = image1;
		}

		//konstruktor item amit modosit, itemtype ami a tipusa
        public ModositFilm(String item, String itemType)
        {
			try
			{
				myReader = new MyReader(ref Err);

				itemToModify = item;
				itemTypeToModify = itemType;

				if (item != null)
					switch (itemTypeToModify)
					{
						case "Film":
							itemToModifyID = myReader.getFilmIDByName(item);
							break;

						case "Szinesz":
							itemToModifyID = myReader.getSzineszIDByName(item);
							break;

						case "Rendezo":
							itemToModifyID = myReader.getRendezoIDByName(item);
							break;
					}
				else
					itemToModifyID = -1;

				InitializeComponent();
				//loadBgImage();

				//atvalltsa a dolgokat
				changeState(itemTypeToModify);
				changeStateListViews(item);

				if (item == null)
					loadDataNew();
				else
					loadDataModify();
				
			}
			catch (Exception e1)
			{
				MessageBox.Show("Hiba. Kerem inditsa ujra a programot." + e1.Message);
			}
        }

        public ModositFilm(String item, String itemType,bool megtekint)
        {
            
            try
            {
                myReader = new MyReader(ref Err);

                itemToModify = item;
                itemTypeToModify = itemType;

                if (item != null)
                    switch (itemTypeToModify)
                    {
                        case "Film":
                            itemToModifyID = myReader.getFilmIDByName(item);
                            break;

                        case "Szinesz":
                            itemToModifyID = myReader.getSzineszIDByName(item);
                            break;

                        case "Rendezo":
                            itemToModifyID = myReader.getRendezoIDByName(item);
                            break;
                    }
                else
                    itemToModifyID = -1;

                InitializeComponent();
                //loadBgImage();
                hideControlls();

                //atvalltsa a dolgokat
                changeState(itemTypeToModify);
                changeStateListViews(item);

                if (item == null)
                    loadDataNew();
                else
                    loadDataModify();
            }
            catch (Exception e1)
            {
                MessageBox.Show("Hiba. Kerem inditsa ujra a programot." + e1.Message);
            }
        }

		//megjeleniti a formot uj adatok bevitelere
		public void loadDataNew()
		{
			switch (itemTypeToModify)
			{
				case "Film":
					myReader.feltoltCombo(MikorCombo);
					myReader.feltoltListViewModositasban(listView1, listView2, listView3, listView4, "Film");
				break;

				case "Szinesz":
					//myReader.feltoltCombo(MikorCombo);
					myReader.feltoltListViewModositasban(listView1, listView2, listView3, listView4, "Szinesz");
				break;

				case "Rendezo":
					//myReader.feltoltCombo(MikorCombo);
					myReader.feltoltListViewModositasban(listView1, listView2, listView3, listView4, "Rendezo");
				break;
			}
		}

		//megjeleniti a formot modositasra
        public void loadDataModify()
        {
            switch (itemTypeToModify)
			{
				case "Film":
					myReader.feltoltDolgok(FilmNevTxt, ArTxt, KiadasiDatumPick, HosszTxt, LeirasTxt, ErtekelesTxt, MikorCombo, radioButton1, radioButton2, itemToModify, listView1, listView2, listView3, listView4, drbTxtBox, "Film");
					
				break;

				case "Szinesz":
				myReader.feltoltDolgok(FilmNevTxt, ArTxt, KiadasiDatumPick, HosszTxt, LeirasTxt, ErtekelesTxt, MikorCombo, radioButton1, radioButton2, itemToModify, listView1, listView2, listView3, listView4, drbTxtBox, "Szinesz");
				break;

				case "Rendezo":
				myReader.feltoltDolgok(FilmNevTxt, ArTxt, KiadasiDatumPick, HosszTxt, LeirasTxt, ErtekelesTxt, MikorCombo, radioButton1, radioButton2, itemToModify, listView1, listView2, listView3, listView4, drbTxtBox, "Rendezo");
				break;
			}
			//mentesRegiErtekek("Film");
        }

		//amikor valtozik a txt mezo (nem hasznalom)
        private void HosszTxt_TextChanged(object sender, EventArgs e)
        {
			/*
            int result;
            if (HosszTxt.Text != "")
            {
                if (int.TryParse(HosszTxt.Text, out result))
                {

                }
                else
                {
                    MessageBox.Show("Csak szamokat lehet beirni");
                    HosszTxt.ResetText();
                }
            }
			*/
        }

		//amikor valtozik a txt mezo (nem hasznalom)
        private void ArTxt_TextChanged(object sender, EventArgs e)
        {
			/*
            int result;
            if (ArTxt.Text != "")
            {
                if (int.TryParse(ArTxt.Text, out result))
                {

                }
                else
                {
                    MessageBox.Show("Csak szamokat lehet beirni");
                    ArTxt.ResetText();
                }
            }
			*/
        }

		//amikor valtozik a txt mezo (nem hasznalom)
        private void ErtekelesTxt_TextChanged(object sender, EventArgs e)
        {
			/*
            float result;
            if (ErtekelesTxt.Text != "")
            {
                if (float.TryParse(ErtekelesTxt.Text, out result))
                {

                }
                else
                {
                    MessageBox.Show("Csak szamokat lehet beirni");
                    ErtekelesTxt.ResetText();
                }
            }
			*/
        }

		//megse gomb
        private void MegseBut_Click(object sender, EventArgs e)
        {
            this.Close();
        }

		//mentes gomb
        private void MentesBut_Click(object sender, EventArgs e)
        {
			if (validInput(itemTypeToModify) == true)
			{
				if (itemToModifyID != -1)
				{
					//modositas
					
					switch (itemTypeToModify)
					{
						case "Film":
							beallitRegiErtekek("Film");
							if (myReader.mentes(FilmNevTxt, ArTxt, KiadasiDatumPick, HosszTxt, LeirasTxt, ErtekelesTxt, MikorCombo, radioButton1, radioButton2, itemToModify, itemToModifyID, "FilmModosit", drbTxtBox))
								this.Close();
							
							break;

						case "Rendezo":
							beallitRegiErtekek("Rendezo");
                            if (myReader.mentes(FilmNevTxt, ArTxt, KiadasiDatumPick, HosszTxt, LeirasTxt, ErtekelesTxt, MikorCombo, radioButton1, radioButton2, itemToModify, itemToModifyID, "RendezoModosit", drbTxtBox))
								this.Close();
							break;

						case "Szinesz":
							beallitRegiErtekek("Szinesz");
                            if (myReader.mentes(FilmNevTxt, ArTxt, KiadasiDatumPick, HosszTxt, LeirasTxt, ErtekelesTxt, MikorCombo, radioButton1, radioButton2, itemToModify, itemToModifyID, "SzineszModosit", drbTxtBox))
								this.Close();
							break;
					}
				}
				else
				{
					//uj
					switch (itemTypeToModify)
					{
						case "Film":
                            if (myReader.mentes(FilmNevTxt, ArTxt, KiadasiDatumPick, HosszTxt, LeirasTxt, ErtekelesTxt, MikorCombo, radioButton1, radioButton2, itemToModify, itemToModifyID, "FilmUj", drbTxtBox))
								this.Close();
							break;

						case "Rendezo":
                            if (myReader.mentes(FilmNevTxt, ArTxt, KiadasiDatumPick, HosszTxt, LeirasTxt, ErtekelesTxt, MikorCombo, radioButton1, radioButton2, itemToModify, itemToModifyID, "RendezoUj", drbTxtBox))
								this.Close();
							break;

						case "Szinesz":
                            if (myReader.mentes(FilmNevTxt, ArTxt, KiadasiDatumPick, HosszTxt, LeirasTxt, ErtekelesTxt, MikorCombo, radioButton1, radioButton2, itemToModify, itemToModifyID, "SzineszUj", drbTxtBox))
								this.Close();
							break;
					}
				}
				//myReader.feltoltListView(mySearch, searchByCombo, listView1);
			}
     
        }

		//elso hozzaadas gomb
        private void hozzaad_rendezo_Click(object sender, EventArgs e)
        {
            if (listView1.FocusedItem != null)
            {
                String item = listView1.FocusedItem.Text;

                myReader.hozzaadRendezo(itemToModifyID, item, FilmNevTxt, ArTxt, KiadasiDatumPick, HosszTxt, LeirasTxt, ErtekelesTxt, MikorCombo, radioButton1, radioButton2, itemToModify, listView1, listView2, listView3, listView4, drbTxtBox, itemTypeToModify);
            }
            else
                MessageBox.Show("valasszon ki valamit");
        }

		//masodik hozzaad gomb
        private void hozzaad_szinesz_Click(object sender, EventArgs e)
        {
            if (listView2.FocusedItem != null)
            {
                String item = listView2.FocusedItem.Text;

                myReader.hozzaadSzinesz(itemToModifyID, item, FilmNevTxt, ArTxt, KiadasiDatumPick, HosszTxt, LeirasTxt, ErtekelesTxt, MikorCombo, radioButton1, radioButton2, itemToModify, listView1, listView2, listView3, listView4, drbTxtBox, itemTypeToModify);
            }
            else
                MessageBox.Show("valasszon ki valamit");
        }

		//elso kivesz
        private void kivesz_rendezo_Click(object sender, EventArgs e)
        {
            if (listView3.FocusedItem != null)
            {
                String item = listView3.FocusedItem.Text;

                myReader.kiveszRendezo(itemToModifyID, item, FilmNevTxt, ArTxt, KiadasiDatumPick, HosszTxt, LeirasTxt, ErtekelesTxt, MikorCombo, radioButton1, radioButton2, itemToModify, listView1, listView2, listView3, listView4, drbTxtBox, itemTypeToModify);
            }
            else
                MessageBox.Show("valasszon ki valamit");
        }

		//masodik kivesz
        private void kivesz_szinesz_Click(object sender, EventArgs e)
        {
            if (listView4.FocusedItem != null)
            {
                String item = listView4.FocusedItem.Text;

                myReader.kiveszSzinesz(itemToModifyID, item, FilmNevTxt, ArTxt, KiadasiDatumPick, HosszTxt, LeirasTxt, ErtekelesTxt, MikorCombo, radioButton1, radioButton2, itemToModify, listView1, listView2, listView3, listView4, drbTxtBox, itemTypeToModify);
            }
            else
                MessageBox.Show("valasszon ki valamit");
        }

		
		private void MikorCombo_SelectedIndexChanged(object sender, EventArgs e)
		{

		}

		//valtoztassa a listviewokat
		private void changeStateListViews(String item)
		{
			if (item == null)
			{
				listView1.Visible = false;
				listView2.Visible = false;
				listView3.Visible = false;
				listView4.Visible = false;
			}
		}
		
		//valtoztassa a dolgok visibilityjet attol fugg, h mi van kivalasztva
		private void changeState(String txt)
		{
			switch (txt)
			{
				case "Film":
					FilmNevTxt.Visible = true;
					KiadasiDatumPick.Visible = true;
					HosszTxt.Visible = true;
					LeirasTxt.Visible = true;
					ArTxt.Visible = true;
					ErtekelesTxt.Visible = true;
					MikorCombo.Visible = true;

					label5.Text = "Kiadasi datummmm";
					label2.Text = "Hossz";
					//label6.Text = "Ar";

					listView1.Visible = true;
					listView3.Visible = true;
					listView2.Visible = true;
					listView4.Visible = true;
					break;

				case "Rendezo":
					FilmNevTxt.Visible = true;
					KiadasiDatumPick.Visible = true;
					HosszTxt.Visible = true;
					LeirasTxt.Visible = true;
					ArTxt.Visible = false;
					ErtekelesTxt.Visible = false;
					MikorCombo.Visible = false;

					label5.Text = "Szuletesi Datum";
					label2.Text = "Nemzetiseg";
					label6.Visible = false;
					label4.Visible = false;
					label7.Visible = false;
                    drbLabe.Visible = false;
                    drbTxtBox.Visible = false;
					//label6.Text = "";

					listView2.Visible = false;
					listView4.Visible = false;
					listView1.Visible = true;
					listView3.Visible = true;
					break;

				case "Szinesz":
					FilmNevTxt.Visible = true;
					KiadasiDatumPick.Visible = true;
					HosszTxt.Visible = true;
					LeirasTxt.Visible = false;
					ArTxt.Visible = true;
					ErtekelesTxt.Visible = false;
					MikorCombo.Visible = false;

					label5.Text = "Szuletesi Datum";
					label2.Text = "Nemzetiseg";
					label6.Text = "Szuletesi Hely";
					label4.Visible = false;
					label7.Visible = false;
					label3.Visible = false;

                    drbLabe.Visible = false;
                    drbTxtBox.Visible = false;

					radioButton1.Visible = true;
					radioButton2.Visible = true;
					//label6.Text = "";

					listView1.Visible = false;
					listView3.Visible = false;
					listView2.Visible = true;
					listView4.Visible = true;
					break;
			}
		}

		private void FilmNevTxt_TextChanged(object sender, EventArgs e)
		{

		}

		private void KiadasiDatumPick_TextChanged(object sender, EventArgs e)
		{

		}

		//megnezi ha jok a bemeneti adatok
		private bool validInput(String itemType)
		{

			//return true;

            int result;
			float result2;
			DateTime result3;

			switch(itemType)
			{
				case "Film":
					if (HosszTxt.Text != "")
					{
						if (int.TryParse(HosszTxt.Text, out result))
						{

						}
						else
						{
							MessageBox.Show("Rossz adatok!");
							HosszTxt.ResetText();
							return false;
						}
					}
					else
					{
						MessageBox.Show("Toltsen ki mindent");
						return false;
					}
		
					if (ArTxt.Text != "")
					{
						if (int.TryParse(ArTxt.Text, out result))
						{

						}
						else
						{
							MessageBox.Show("Rossz adatok!");
							ArTxt.ResetText();
							return false;
						}
					}
					else
					{
						MessageBox.Show("Toltsen ki mindent");
						return false;
					}

					if (ErtekelesTxt.Text != "")
					{
						if (float.TryParse(ErtekelesTxt.Text, out result2))
						{

						}
						else
						{
							MessageBox.Show("Rossz adatok!");
							ErtekelesTxt.ResetText();
							return false;
						}
					}
					else
					{
						MessageBox.Show("Toltsen ki mindent");
						return false;
					}

					if (KiadasiDatumPick.Text != "")
					{
						if (DateTime.TryParse(KiadasiDatumPick.Text, out result3))
						{

						}
						else
						{
							MessageBox.Show("Rossz adatok!");
							KiadasiDatumPick.ResetText();
							return false;
						}
					}
					else
					{
						MessageBox.Show("Toltsen ki mindent");
						return false;
					}

				break;

				case "Rendezo":
					if (KiadasiDatumPick.Text != "")
					{
						if (DateTime.TryParse(KiadasiDatumPick.Text, out result3))
						{

						}
						else
						{
							MessageBox.Show("Rossz adatok!");
							KiadasiDatumPick.ResetText();
							return false;
						}
					}
					else
					{
						MessageBox.Show("Toltsen ki mindent");
						return false;
					}
				break;

				case "Szinesz":
					if (KiadasiDatumPick.Text != "")
					{
						if (DateTime.TryParse(KiadasiDatumPick.Text, out result3))
						{

						}
						else
						{
							MessageBox.Show("Rossz adatok!");
							KiadasiDatumPick.ResetText();
							return false;
						}
					}
					else
					{
						MessageBox.Show("Toltsen ki mindent");
						return false;
					}

					if (radioButton1.Checked == false && radioButton2.Checked == false)
					{
						MessageBox.Show("Toltsen ki mindent");
						return false;
					}
				break;
			}

			return true;
		}
        
        private void hideControlls()
        {
            MentesBut.Hide();
            hozzaad_rendezo.Hide();
            hozzaad_szinesz.Hide();
            kivesz_rendezo.Hide();
            kivesz_szinesz.Hide();

            drbTxtBox.Enabled = false;
        }
    
		private void mentesRegiErtekek(String type)
		{
			switch (type)
			{
				case "Film":
					regiAr = Int32.Parse(ArTxt.Text);
					regiErtekeles = float.Parse(ErtekelesTxt.Text);
					regiFilmLeiras = LeirasTxt.Text;
					regiFilmNev = FilmNevTxt.Text; ;
					regiHossz = Int16.Parse(HosszTxt.Text);
					regiKiDat = KiadasiDatumPick.Text;
					regiMostID = MikorCombo.SelectedIndex;
					//MessageBox.Show("efg" + KiadasiDatumPick.Text + "ss");
					break;
				case "Rendezo":
					regiRendezoNev = FilmNevTxt.Text;
					regiRendezoLeiras =LeirasTxt.Text;
					regiRendezoSzulDat= KiadasiDatumPick.Text;
					regiNemzetiseg = HosszTxt.Text;

				break;
				case "Szinesz":
				regiSzineszNev = FilmNevTxt.Text;
				regiSzineszSzulDat = KiadasiDatumPick.Text;
				regiSzineszSzulHely = ArTxt.Text;
				regiNemzetiseg = HosszTxt.Text;
				if (radioButton1.Checked == true)
					regiNem = 0; //ferfi
				else
					regiNem = 1;
				break;
			}
		}

		private void beallitRegiErtekek(string type)
		{
			switch(type){
					case "Film":
						myReader.regiFilmNev = regiFilmNev;
						myReader.regiFilmLeiras = regiFilmLeiras;
						myReader.regiKiDat = regiKiDat;
						myReader.regiAr = regiAr;
						myReader.regiErtekeles = regiErtekeles;
						myReader.regiHossz = regiHossz;
						myReader.regiMostID = regiMostID;
						break;
					case "Rendezo":
						myReader.regiRendezoNev=regiRendezoNev;
						myReader.regiRendezoSzulDat=regiRendezoSzulDat;
						myReader.regiRendezoLeiras = regiRendezoLeiras;
						myReader.regiNemzetiseg  = regiNemzetiseg;
						break;
				case "Szinesz":
						myReader.regiSzineszNev = regiSzineszNev;
						myReader.regiSzineszSzulDat = regiSzineszSzulDat;
						myReader.regiSzineszSzulHely = regiSzineszSzulHely;
						myReader.regiNemzetiseg = regiNemzetiseg;
						myReader.regiNem = regiNem;
						break;

		}

			//MessageBox.Show(regiKiDat + "asdasd");
		}

		private void ModositFilm_Shown(object sender, EventArgs e)
		{

			switch (itemTypeToModify)
			{ 
				case "Film":
					mentesRegiErtekek("Film");
					break;
				case "Rendezo":
					mentesRegiErtekek("Rendezo");
					break;
				case "Szinesz":
					mentesRegiErtekek("Szinesz");
					break;

			}
			
		}

        private void ModositFilm_Load(object sender, EventArgs e)
        {
            if (itemToModifyID != -1)
            {
                switch (itemTypeToModify)
                {
                    case "Film":
                        mentesRegiErtekek("Film");
                        break;
                    case "Rendezo":
                        mentesRegiErtekek("Rendezo");
                        break;
                    case "Szinesz":
                        mentesRegiErtekek("Szinesz");
                        break;

                }
            }
        }
	}
}
